﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using INTEX.DataBase;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class JLTONAV95 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        DataSet setDISPO;
        double FLeng = 0, GLeng = 0, WLeng = 0;
        int FLeadTime = 0, GLeadTime = 0, WLeadTime = 0, YLeadTime = 14;
        double Fnum = 0, Gnum = 0, Wnum = 0;
        string FEnd = "", GEnd = "", WEnd = "", YEnd = "";
        string DISPONUM = "";
        DataRow lastrow = null; 
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        
        JLTONAV95Data m_Data, m_DataX;
        JLTONAV95Report m_rpt;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        string UserID = "";
        CollectionViewSource vwWASTAMM
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwWASTAMM");
            }
        }
        DataRow dwWASTAMM
        {
            get
            {
                if (vwWASTAMM.View == null)
                    return null;
                if ((DataRowView)vwWASTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWASTAMM.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if ((DataRowView)vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        string KRS
        {
            get
            {
                return m_Data.Left(txtVKPAUFKRS.Text.Trim() + "  ", 2);
            }
            set
            {
                txtVKPAUFKRS.Text = m_Data.Left(txtVKPAUFKRS.Text.Trim() + value, 2);
            }
        }
        string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtVKPAUFLFD.Text.Trim(), 6);
            }
            set
            {
                txtVKPAUFLFD.Text = m_Data.Right("000000" + value, 6); ;
            }
        }
        string POS
        {
            get
            {
                return m_Data.Right("000" + txtVKPAUFPOS.Text.Trim(), 3);
            }
            set
            {
                txtVKPAUFPOS.Text = m_Data.Right("000" + value, 3);
            }
        }
        public decimal LDAB05
        {
            get
            {
                return Convert.ToDecimal(txtLDAB05.Text.ToString());
            }
        }
        public decimal LDAB55
        {
            get
            {
                return Convert.ToDecimal(txtLDAB55.Text.ToString());
            }
        }
        public decimal LDAB45
        {
            get
            {
                return Convert.ToDecimal(txtLDAB45.Text.ToString());
            }
        }
        public decimal LDAB50
        {
            get
            {
                return Convert.ToDecimal(txtLDAB50.Text.ToString());
            }
        }
        public JLTONAV95()
        {
            InitializeComponent();
            this.Name = "计划编排";
            this.Title = "计划编排";
        }
        #endregion
        #region Events
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            vwVKPOS.View.CurrentChanged += vwVKPOS_CurrentChanged;
        }
        void vwVKPOS_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwVKPOS == null)
                    return;
                if (lastrow == dwVKPOS)
                    return;
                lastrow = dwVKPOS;
                Prepare();
                DataSet SResult=m_Data.PrePlan(dwVKPOS);
                SetProd.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                base.SetUp(dicParamater);
                InitializeComponent();
                this.dicParamater = dicParamater;
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                m_Data = new JLTONAV95Data(OracleDB, proxy);
                txtUserID.Text = (string)dicParamater["User"];
                txtPassword.Password = (string)dicParamater["Pwd"];
                m_Data.UserID = Helpers.Helper.Left((string)dicParamater["User"] + "           ", 8);
                KRS = "J3";
                LFD = "002006";
                ReloadCon();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
            if (setResult.Tables.Count > 0)
            {
                DataRow dwUser = setResult.Tables[0].Rows[0];
                txtSPRACHKZ.Text = dwUser["HRSPRKZ"].ToString();
                UserID = userID;
                m_Data.UserID = userID;
                this.UserID = userID;

                m_Data.WorkingDate = DateTime.Now.Year.ToString() + m_Data.Right("00" + DateTime.Now.Month.ToString(), 2)
                    + m_Data.Right("00" + DateTime.Now.Day.ToString(), 2);
                m_Data.MANDANT = Mandant;
                m_Data.SPRACHKZ = txtSPRACHKZ.Text.Trim();
                this.txtLogIn.Text = DateTime.Now.ToLocalTime().ToString() + ": " + userID + "已登录";
                txtLogIn.Background = new SolidColorBrush(Colors.Green);

            }
        }

        #endregion

        #region==========AV95ON======================
        public bool Prepare()
        {
            bool result = true;
            try
            {
                string filter = "";
                DataRow[] rows = null;
                this.SetProd.ABPOS.Clear();
                DataTable tb = null;

                DataRow dwVKWDECK = null; 
                filter = "WDAUFKRS='" + dwVKPOS["VKPAUFKRS"].ToString() + "' AND WDAUFLFD='"
                    + dwVKPOS["VKPAUFLFD"].ToString() + "' AND WDAUFPOS='" + dwVKPOS["VKPAUFPOS"].ToString() + "' AND WDSA='K' AND WDUPOS='00'";
                rows = SetData.WDECK.Select(filter);
                if (rows.Length > 0)
                {                    
                    tb = rows.CopyToDataTable();
                    tb.TableName = "WDECK";
                    SetProd.Merge(tb);
                }
                foreach (DataRow row in SetProd.Tables["WDECK"].Rows)
                {
                    dwVKWDECK = row;
                }
                filter = "WDAUFKRS='" + dwVKPOS["VKPAUFKRS"].ToString() + "' AND WDAUFLFD='" + dwVKPOS["VKPAUFLFD"].ToString()
                 + "' AND WDAUFPOS='" + dwVKPOS["VKPAUFPOS"].ToString() + "'  AND WDSA='K' AND WDUPOS='00'";
                rows = SetData.WASTAMM.Select(filter);
                if (rows.Length > 0)
                {
                    tb = rows.CopyToDataTable();
                    tb.TableName = "WASTAMM";
                    SetProd.Merge(tb);
                }      
                if (vwWASTAMM.View != null)
                {
                    vwWASTAMM.View.MoveCurrentToFirst();
                } 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        } 
        #endregion

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetData.Clear();
                DataSet SResult = new DataSet();
                DataRow dwData = SetData.VKPOS.NewRow();
                dwData["VKPMANDANT"] = "00";
                dwData["VKPAUFKRS"] = KRS;
                dwData["VKPAUFLFD"] = LFD;
                SResult = m_Data.LoadData(dwData);
                foreach(DataTable tb in SResult.Tables)
                {
                    string tbname = tb.TableName;
                    SetData.Merge(tb);
                }
                m_Data.SetData = SetData;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ReloadCon()
        {
            DataSet SResult = null;
            SResult = m_Data.LoadEnv();
            SetEnv.Merge(SResult);
        }
        private void btnInsertt_Click(object sender, RoutedEventArgs e)
        {
            SetProd.VKPOS.Clear();
            SetProd.VKPOS.Merge(SetData.VKPOS);
            SetProd.APFOLGE.Clear();
            SetProd.APFOLGE.Merge(SetData.APFOLGE);
            SetProd.APARTK.Clear();
            SetProd.APARTK.Merge(SetData.APARTK);
            m_Data.AB2ON(SetProd);
        }
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet SResult = null;
                SResult = m_Data.PrePlan(dwVKPOS);
                SetProd.Clear();
                SetProd.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnDeleteABON_Click(object sender, RoutedEventArgs e)
        {
            m_Data.DeleteABON(KRS, LFD, POS);
            return;
            m_Data.Delete("00", KRS, LFD, POS);
        }
        private void txtVKPAUFLFD_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                btnQuery_Click(null, null);
            }
        }
    }
}
